<template>
  <Panel>
    <template #title>管理员信息</template>
    <template #content>
      <ul>
        <li>
          管理员ID: <span>{{ userData.id }}</span>
        </li>
        <li>
          账号: <span>{{ userData.account }}</span>
        </li>
        <li>
          用户组: <span>{{ userData.userGroup }}</span>
        </li>
        <li>
          创建时间: <span>{{ userData.ctime | dataForat }}</span>
        </li>
      </ul>

      <div class="imgbox">
        <p>管理员头像</p>
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/users/avatar_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-button
          v-waves
          type="primary"
          @click="saveImg()"
          size="mini"
          v-show="imageUrl"
          >上传<i class="el-icon-upload el-icon--right"></i
        ></el-button>
      </div>
    </template>
  </Panel>
</template>

<script>
/* 引入本地存储 */
import local from '@/utils/local'
import { alterImg } from '@/api/user'
export default {
  data() {
    return {
      userData: {},
      imageUrl: '',
      imgUrl: ''
    }
  },
  created() {
    this.getuser()
  },
  methods: {
    /* 个人信息 */ getuser() {
      const accountInfo = JSON.parse(local.get('accountInfo'))
      this.userData = accountInfo
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.imgUrl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    /* 保存头像 */
    async saveImg() {
      const { code } = await alterImg({ imgUrl: this.imgUrl })
      if (code === 0) setTimeout(() => window.location.reload(), 1500)
    }
  }
}
</script>

<style lang="less" scoped>
li {
  height: 60px;
  line-height: 60px;
  border-bottom: 1px solid #eee;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-left: 10px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}

.imgbox {
  display: flex;
  align-items: center;
  padding: 10px;
}
/deep/.el-button {
  align-self: flex-end;
  margin-left: 20px;
}
</style>
